package com.greatwall.hip.cms.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.greatwall.hip.cms.controller.BaseController;
import com.greatwall.hip.cms.entity.LyMzYblxmx;
import com.greatwall.hip.cms.entity.LyMzYbqk;
import com.greatwall.hip.cms.entity.LyZyYblxmx;
import com.greatwall.hip.cms.entity.LyZyYbqk;
import com.greatwall.hip.cms.model.Req;
import com.greatwall.hip.cms.model.Result;
import com.greatwall.hip.cms.service.LyMzYblxmxService;
import com.greatwall.hip.cms.service.LyMzYbqkService;
import com.greatwall.hip.cms.service.LyZyYblxmxService;
import com.greatwall.hip.cms.service.LyZyYbqkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName OutpatientController
 * @Description TODO
 * @Author zhous
 * @Date 2021/7/5 11:34
 */
@RestController
@RequestMapping("/insurance/inpatient")
@Api(tags = "医保模块", value = "医保住院")
public class InpatientController extends BaseController {

    @Autowired
    private LyZyYblxmxService lyZyYblxmxService;

    @Autowired
    private LyZyYbqkService lyZyYbqkService;

    @PostMapping("/basicInfo")
    @ApiOperation("基础情况")
    public Result<LyZyYbqk> getBasicInfo(@RequestBody Req req){


        LyZyYbqk data = lyZyYbqkService.selectBasicInfo(req);
        if(null == data ){
            data = new LyZyYbqk();

        }        //人均住院费用
        BigDecimal avgAmount = 0 == data.getPatientCount() ? BigDecimal.ZERO : new BigDecimal(data.getTotalAmount()).divide(new BigDecimal(data.getPatientCount()), 2, BigDecimal.ROUND_HALF_UP);
        data.setAvgAmount(avgAmount);
        //人均医保费用
        BigDecimal avgInsuranceAmount = 0 == data.getInsurancePatientCount() ? BigDecimal.ZERO : new BigDecimal(data.getInsuranceAmount()).divide(new BigDecimal(data.getInsurancePatientCount()), 2, BigDecimal.ROUND_HALF_UP);
        data.setAvgInsuranceAmount(avgInsuranceAmount);
        //人均报销比例
        BigDecimal avgInsuranceRate = avgAmount.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : avgInsuranceAmount.divide(avgAmount,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
        data.setAvgInsuranceRate(avgInsuranceRate);

        return Result.success(data);
    }

    @PostMapping("/getInsuranceAmountTrend")
    @ApiOperation("历史医保费用变化趋势")
    public Result<Map<String,Object>> getInsuranceAmountTrend(){
        Req req = new Req();
        List<String> dateList = getNearYearMonthDate();
        req.setStartDate(dateList.get(0));
        req.setEndDate(dateList.get(dateList.size()-1));
        req.setType("2");
        List<LyZyYbqk> dataList = lyZyYbqkService.selectNearYearTrend(req);
        List<LyZyYbqk> result = Lists.newArrayList();
        Map<String,Object> resultMap = Maps.newHashMap();
        //为未查出来的月份补默认值0
        for(String date : dateList ){
            LyZyYbqk vo = dataList.stream().filter(x -> date.equals(x.getDates())).findFirst().orElse(null);
            if(null == vo){
                LyZyYbqk vi = new LyZyYbqk();
                vi.setDates(date);
                vi.setInsuranceAmount(0d);
                vi.setRate(BigDecimal.ZERO);
                result.add(vi);
            }else {
                result.add(vo);
            }
        }
        resultMap.put("dates",dateList);
        resultMap.put("dataList",result);
        return Result.success(resultMap);
    }

    @PostMapping("/getInsurancePatientCountTrend")
    @ApiOperation("历史医保人数变化趋势")
    public Result<Map<String,Object>> getInsurancePatientCountTrend(){
        Req req = new Req();
        List<String> dateList = getNearYearMonthDate();
        req.setStartDate(dateList.get(0));
        req.setEndDate(dateList.get(dateList.size()-1));
        req.setType("2");
        List<LyZyYblxmx> list = lyZyYblxmxService.selectInsurancePatientTrend(req);
        //为前端封装参数格式
        Map<String, List<LyZyYblxmx>> collect = list.stream().collect(Collectors.groupingBy(LyZyYblxmx::getKeyName));
        List<Map<String,Object>> result = Lists.newArrayList();
        Map<String,Object> resultMap = Maps.newHashMap();
        collect.keySet().forEach(d -> {
            Map<String,Object> map = Maps.newHashMap();
            map.put("name",d);
            List<Integer> patientList = Lists.newArrayList();
            //为未查出来的月份补默认值0
            for(String date : dateList ){
                LyZyYblxmx vo = collect.get(d).stream().filter(x -> date.equals(x.getDates())).findFirst().orElse(null);
                if(null == vo){
                    patientList.add(0);
                }else {
                    patientList.add(vo.getNumber());
                }
            }
            map.put("data",patientList);
            result.add(map);

        });
        resultMap.put("dataList",result);
        resultMap.put("dates",dateList);
        return Result.success(resultMap);
    }


    @PostMapping("/getInsuranceDetail")
    @ApiOperation("医保费用构成及各类医保对比")
    public Result getInsuranceDetail(@RequestBody Req req){

        LyZyYbqk data = lyZyYbqkService.selectBasicInfo(req);
        if(null == data ){
            data = new LyZyYbqk();
        }

        List<LyZyYblxmx> lyZyYblxmxes = lyZyYblxmxService.selectInsuranceAmount(req);

        Map<String,Object> result = Maps.newHashMap();

        //医疗总费用
        double totalAmount = data.getTotalAmount();
        result.put("totalAmount",totalAmount);

        //各类支付方式支付费用
        List<Map<String,Object>> amountTypeList = Lists.newArrayList();
        Map<String,Object> medicalInsurancePay = Maps.newHashMap();
        medicalInsurancePay.put("name","基本医疗保险");
        medicalInsurancePay.put("value",data.getMedicalInsurancePay());
        amountTypeList.add(medicalInsurancePay);
        Map<String,Object> bizInsurancePay = Maps.newHashMap();
        bizInsurancePay.put("name","商保支付");
        bizInsurancePay.put("value",data.getBizInsurancePay());
        amountTypeList.add(bizInsurancePay);
        Map<String,Object> oneStationPay = Maps.newHashMap();
        oneStationPay.put("name","一站式结算支出");
        oneStationPay.put("value",data.getOneStationPay());
        amountTypeList.add(oneStationPay);
        Map<String,Object> cashPay = Maps.newHashMap();
        cashPay.put("name","个人现金支付");
        cashPay.put("value",data.getCashPay());
        amountTypeList.add(cashPay);

        result.put("amountTypeList",amountTypeList);

        //各类医保类型
        if(CollectionUtils.isNotEmpty(lyZyYblxmxes)){
            List<Map<String,Object>> patientCountList = Lists.newArrayList();
            List<Map<String,Object>> insuranceAmountList = Lists.newArrayList();
            List<Map<String,Object>> insuranceRateList = Lists.newArrayList();
            for (LyZyYblxmx vo : lyZyYblxmxes){
                //各类医保补偿人次数
                Map<String,Object> patientCount = Maps.newHashMap();
                patientCount.put("name",vo.getKeyName());
                patientCount.put("value",vo.getNumber());
                patientCountList.add(patientCount);
                //各类医保报销金额
                double amount = null == vo.getAmount() ? 0d : vo.getAmount();
                Map<String,Object> insuranceAmount = Maps.newHashMap();
                insuranceAmount.put("name",vo.getKeyName());
                insuranceAmount.put("value",amount);
                insuranceAmountList.add(insuranceAmount);
                //各类医保报销比例
                Map<String,Object> insuranceRate = Maps.newHashMap();
                insuranceRate.put("name",vo.getKeyName());
                insuranceRate.put("value",0d == totalAmount? 0d : new BigDecimal(amount).divide(new BigDecimal(totalAmount),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")));
                insuranceRateList.add(insuranceRate);
            }
            result.put("patientCountList",patientCountList);
            result.put("insuranceAmountList",insuranceAmountList);
            result.put("insuranceRateList",insuranceRateList);

        }
        return Result.success(result);

    }


}
